require([
  'jquery',
  'pageInit',
  'dataTableFilter',
  'loading',
  'notification',
  'listAsyncOp'
], function($, vamakerPage, VamDataTable, VamLoader, VamOpMsg, VamListOp) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      dataUrl = VAM.getVar('PROFIT_SETTING_DATA_LIST'),
      dataProfitOp = VAM.getVar('PROFIT_SETTING_URL'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    // Table settings.
    tableOptions = {
      'ajax': {
        'url': dataUrl
      },
      'order': [7,'desc'],
      'columns': [
        {'data': 'id'},
        {'data': 'name'},
        {'data': 'status'},
        {'data': 'advertiser'},
        {'data': 'startDate'},
        {'data': 'endDate'},
        {'data': 'launchTotalBudget'},
        // {'data': 'initCurrMargin'},
	{'data': 'lastModified'},
        {'data': 'tomorrowMargin'}
      ],
      'columnDefs': [
        {
          'targets': [0,1,3,4,5,7],
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '">' + data +'</p>'
          }
        },
        {
          'targets': 2,
          'render': function (data, type, full, meta) {
            var result = '';
            if (data === 'archived') {
              result = VAM.j('Archived');
            } else if (data === 'effective') {
              result = VAM.j('Effective');
            } else if (data === 'waiting') {
              result = VAM.j('Waiting');
            } else if (data === 'paused') {
              result = VAM.j('Paused');
            } else if (data === 'closed') {
              result = VAM.j('Closed');
            } else if (data === 'incomplete') {
              result = VAM.j('Incomplete');
            }
            ;
            return '<p title="' + result + '">' + result +'</p>';
          }
        },
        {
          'targets': -3,
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '"><i class="fa fa-rmb"></i> ' + data +'</p>'
          }
        },
        // {
        //  'targets': -2,
        //  'render': function (data, type, full, meta) {
        //    return '<p title="' + data + '">' + data +' %</p>'
        //  }
        // },
        {
          'targets': -1,
          'render': function (data, type, full, meta) {
            var status = full.status, result,
              feedback = '<i class="fa fa-check form-control-feedback" title="' + VAM.j('Please enter the correct values.') + '"></i><i class="fa fa-times-circle form-control-feedback"></i>';
            if (status === 'effective') {
              result = '<input class="form-control input-sm set-margin" data-id="' + full.id + '" type="text" min="0" max="100" step="0.1" value="' + data + '" placeholder="">' + feedback + ' %';
            } else if (status === 'archived') {
              result = '<p class="form-control-static input-sm">--</p>';
            } else if (status === 'closed') {
              result = '<p class="form-control-static input-sm">--</p>';
            } else if (status === 'waiting' || status === 'incomplete') {
              result = '<input class="form-control input-sm set-margin" data-id="' + full.id + '" type="text" min="0" max="100" step="0.1" value="' + data + '" placeholder="">' + feedback + ' %';
            } else if (status === 'paused') {
              result = '<input class="form-control input-sm set-margin" data-id="' + full.id + '" type="text" min="0" max="100" step="0.1" value="' + data + '" placeholder="">' + feedback + ' %';
            };
            return '<div class="tab-btn-group">'
                  + '<form class="form-inline" role="form">'
                  + '<div class="form-group has-feedback">'
                  + result
                  + '</div></form>'
                  + '</div>';
          }
        }
      ],
      'initComplete': function() {
        tableResize = new VamDataTable().columnResize($dataTable);
      },
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    $dataTable.on('change', 'tbody td .set-margin', function (evt) {
      var $me = $(this),
        $parent = $me.parent(),
        id = $me.data('id'),
        newValue = $me.val(),
        opData;
        opData = {
          id: id,
          newValue: newValue,
          rnd: (new Date()).getTime()
        };
        if(!/^[0-9]+$/.test(newValue) || parseInt(newValue) > 100 || parseInt(newValue) < 0){
            $parent.addClass('has-error');
            $parent.removeClass('has-success');
            return false;
        };
      VamListOp.listOp({
        url: dataProfitOp,
        data: opData,
        before: function () {
          $me.prop('readonly', true);
        },
        success: function (data) {
          if (data.status === 'success') {
            VamOpMsg.messager(data);
            $parent.removeClass('has-error');
            $parent.addClass('has-success');
          } else {
            VamOpMsg.messager(data);
            $parent.removeClass('has-success');
            $parent.addClass('has-error');
          }
        },
        always: function () {
          $me.prop('readonly', false);
        }
      });
    });
  });
});
